package ly.count.ohos.sdk;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.rpc.RemoteException;

public class ModuleConsent extends ModuleBase {
    Consent consentInterface = null;

    ModuleLog moduleLog;

    ModuleConsent(Countly cly, CountlyConfig config) {
        super(cly);

        moduleLog = cly.L;

        moduleLog.v("[ModuleConsent] Initialising");

        consentInterface = new Consent();
    }

    @Override
    void initFinished(CountlyConfig config) {
        if (mCly.requiresConsent) {
            // do delayed push consent action, if needed
            if (mCly.delayedPushConsent != null) {
                mCly.doPushConsentSpecialAction(mCly.delayedPushConsent);
            }

            // remove persistent push flag if no push consent was set
            if (!mCly.featureConsentValues.containsKey(Countly.CountlyFeatureNames.push)) {
                mCly.doPushConsentSpecialAction(false);
            }

            // do delayed location erasure, if needed
            if (mCly.delayedLocationErasure) {
                mCly.doLocationConsentSpecialErasure();
            }

            // send collected consent changes that were made before initialization
            if (mCly.collectedConsentChanges.size() != 0) {
                for (String changeItem : mCly.collectedConsentChanges) {
                    mCly.connectionQueue_.sendConsentChanges(changeItem);
                }
                mCly.collectedConsentChanges.clear();
            }

            try {
                Intent intent = new Intent();
                Intent.OperationBuilder operationBuilder = new Intent.OperationBuilder();
                Operation operation = operationBuilder.withAction(mCly.CONSENT_BROADCAST).build();
                intent.setOperation(operation);
                CommonEventData commonEventData = new CommonEventData(intent);
                CommonEventManager.publishCommonEvent(commonEventData);
            } catch (RemoteException e) {
                moduleLog.i("[Consent] RemoteException");
            }
            if (moduleLog.logEnabled()) {
                moduleLog.d("[ModuleConsent] [Init] Countly is initialized with the current consent state:");
                mCly.checkAllConsent();
            }
        }
    }

    @Override
    void halt() {
        consentInterface = null;
    }

    public class Consent {
        /**
         * Print the consent values of all features
         *
         */
        public void checkAllConsent() {
            synchronized (mCly) {
                moduleLog.i("[Consent] calling checkAllConsent");

                mCly.checkAllConsent();
            }
        }

        /**
         * Get the current consent state of a feature
         *
         * @param featureName the name of a feature for which consent should be checked
         * @return the consent value
         */
        public boolean getConsent(String featureName) {
            synchronized (mCly) {
                return mCly.getConsent(featureName);
            }
        }

        /**
         * Remove consent for all features
         *
         */
        public void removeConsentAll() {
            synchronized (mCly) {
                mCly.removeConsentAll();
            }
        }

        /**
         * Remove the consent of a feature
         *
         * @param featureNames the names of features for which consent should be removed
         */
        public void removeConsent(String[] featureNames) {
            synchronized (mCly) {
                mCly.removeConsent(featureNames);
            }
        }

        /**
         * Gives consent for all features
         *
         */
        public void giveConsentAll() {
            synchronized (mCly) {
                moduleLog.i("[Consent] Giving consent for all features");

                if (!mCly.isInitialized()) {
                    moduleLog.w("[Consent] Calling this before initialising the SDK is deprecated!");
                }

                mCly.giveConsent(mCly.validFeatureNames);
            }
        }

        /**
         * Give the consent to a feature
         *
         * @param featureNames the names of features for which consent should be given
         */
        public void giveConsent(String[] featureNames) {
            synchronized (mCly) {
                mCly.giveConsent(featureNames);
            }
        }

        /**
         * Set the consent of a feature
         *
         * @param featureNames feature names for which consent should be changed
         * @param isConsentGiven the consent value that should be set
         */
        public void setConsent(String[] featureNames, boolean isConsentGiven) {
            synchronized (mCly) {
                mCly.setConsentInternal(featureNames, isConsentGiven);
            }
        }

        /**
         * Set the consent of a feature group
         *
         * @param groupName name of the consent group
         * @param isConsentGiven the value that should be set for this consent group
         */
        public void setConsentFeatureGroup(String groupName, boolean isConsentGiven) {
            synchronized (mCly) {
                mCly.setConsentFeatureGroup(groupName, isConsentGiven);
            }
        }

        /**
         * Group multiple features into a feature group
         *
         * @param groupName name of the consent group
         * @param features array of feature to be added to the consent group
         */
        public void createFeatureGroup(String groupName, String[] features) {
            synchronized (mCly) {
                mCly.createFeatureGroup(groupName, features);
            }
        }
    }
}
